global class AT_ProcessAllOpportunities implements Database.Batchable<Sobject>
{
	global string query;

	global AT_ProcessAllOpportunities(string q)
	{
		query = q;
	}
	
	global AT_ProcessAllOpportunities()
	{
		query = 'Select Id,AccountId,CloseDate,Amount,Location_Name__c,Class_Name__c From Opportunity where StageName=\'Closed Won\'';
		//query = 'Select RecordType.Name, RecordTypeId, Id, Latest_Fund_Amount__c, Latest_Fund_Amount_Rank__c, Latest_Fund_Amount_Rank_X_Wtg__c From Account where RecordType.DeveloperName=\'Investor\' and Latest_Fund_Amount__c>0 order by Latest_Fund_Amount__c';
	}

	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}
	
	global void execute(Database.BatchableContext BC, List<sObject> scope)
	{
		List<Opportunity> listOpp2Procc = new List<Opportunity>();
		Set<ID> setIDs2Procc = new Set<ID>();
		for(sObject item:scope)
		{
			listOpp2Procc.add((Opportunity)item);
			setIDs2Procc.add(item.ID);
		}
		if(listOpp2Procc.size()>0) AT_Opportunity_Trigger_Handler.createTransObjs(listOpp2Procc, setIDs2Procc);
		
	}
	
	global void finish(Database.BatchableContext BC)
	{
		
	}	
	
	public static testMethod void testIt()
	{

		ID RT_Account_B2B = [select ID from RecordType r where SobjectType='Account' and DeveloperName='B2B' limit 1].ID;
        AT_sendIntacctCallOut.isTest = true;
 		Account acc = new Account(RecordTypeId=RT_Account_B2B, Name='test Account B2B', BillingCountry='USA');
 		insert acc;
		
		ID RT_Opportunity_Inc = [select ID from RecordType r where SobjectType='Opportunity' and DeveloperName='Inc' limit 1].ID;
		Opportunity opp = new Opportunity(Name='Test Opp - 1', AccountId=acc.ID, CurrencyIsoCode='USD', CloseDate=date.today(), RecordTypeId=RT_Opportunity_Inc);
		opp.StageName = 'Closed Won';
		opp.Probability = 100;
		opp.Class_Name__c = 'RESELLERS';
		opp.Location_Name__c = 'SAS_USA'; 
		insert opp;
		
		List<sObject> list2Test = new List<sObject>();
		list2Test.add(opp);
		
		System.Test.startTest();
		
		AT_ProcessAllOpportunities controller = new AT_ProcessAllOpportunities();
		controller = new AT_ProcessAllOpportunities('Select Id From Opportunity where StageName=\'Closed Won\' LIMIT 1');
		
		Database.BatchableContext BC2 = null;
		controller.start(BC2);
		controller.execute(BC2,list2Test);
		controller.finish(BC2);

		System.Test.stopTest();

	}	
	
}